Responsive Images with srcset and <picture>
Responsive images in HTML allow browsers to load the most appropriate image depending on the device screen size, resolution, or other conditions. Two main tools for this are the srcset attribute (used on <img>) and the <picture> element.
srcset on <img>: Lets you define multiple versions of the same image at different resolutions or sizes. The browser automatically chooses the best one based on screen size and pixel density.
sizes attribute: Works with srcset to tell the browser how much space the image will take up in different layouts.
<picture> element: Gives more control by letting you specify multiple image sources with <source> elements. Useful for art direction (e.g., showing a cropped image on mobile vs. full image on desktop).
Fallback <img>: Inside <picture>, the <img> tag provides the default image if none of the <source> conditions match.
In short: Use srcset + sizes when you only need different image resolutions, and use <picture> when you need different images entirely depending on screen size or context.